package com.yonyou.ucf.mdf.app.controller.isv;


import com.yonyou.ucf.mdf.app.common.ResultMessage;
import com.yonyoucloud.iuap.ucf.mdd.starter.ucg.openapi.OpenApiProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

import static com.yonyou.ypd.bill.utils.HttpUtils.renderJson;

@Slf4j
@RestController
@RequestMapping("/openapi")
public class OpenAppKeyInfoController{

    @Autowired
    private OpenApiProperties properties;

    @Value("${spring.application.name:${vcap.application.name:${spring.config.name:application}}}")
    private String applicationName;

    @GetMapping("/appcode")
    public void getAppCode(@RequestParam(value = "domain", required = true) String domain, HttpServletRequest request,
                           HttpServletResponse response) {
        if (domain.equals(applicationName)) {
            HashMap<String, String> resultMap = new HashMap<>();
            resultMap.put("appCode", properties.getAppCode());
            resultMap.put("applicationName", domain);
            renderJson(response, ResultMessage.data(resultMap));
        } else {
            renderJson(response, ResultMessage.error("获取appCode失败，domain非法"));
        }
    }

    @GetMapping("/isvAppKey")
    public void getisvAppKey(@RequestParam(value = "domain", required = true) String domain, HttpServletRequest request,
                             HttpServletResponse response) {

        if (domain.equals(applicationName)) {
            HashMap<String, String> resultMap = new HashMap<>();
            resultMap.put("isvAppKey", properties.getIsvAppKey());
            resultMap.put("applicationName", domain);
            renderJson(response, ResultMessage.data(resultMap));
        } else {
            renderJson(response, ResultMessage.error("获取appCode失败，domain非法"));
        }
    }

}
